package com.slofzx.nuoda.evaluation.project.excel.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.slofzx.nuoda.evaluation.project.dto.ProjectYearDataExcelDTO;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;

// 有个很重要的点 ExcelListener 不能被spring管理，要每次读取excel都要new,然后里面用到spring可以构造方法传进去
public class ProjectYearDataExcelListener extends AnalysisEventListener<ProjectYearDataExcelDTO> {

    private List<ProjectYearDataExcelDTO> dataList = new ArrayList<>();

    /**
     * 这个每一条数据解析都会来调用
     */
    @Override
    public void invoke(ProjectYearDataExcelDTO model, AnalysisContext analysisContext) {
        if(StringUtils.isNotBlank(model.getItemType())){
            if(StringUtils.isBlank(model.getProjectName()) && dataList != null && !dataList.isEmpty()){
                model.setProjectName(dataList.get(dataList.size() - 1).getProjectName());
            }
            dataList.add(model);
        }
    }

    /**
     * 所有数据解析完成了 都会来调用
     */
    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {}

    public List<ProjectYearDataExcelDTO> getDataList() {
        return dataList;
    }

}
